`
https://leetcode.cn/problems/find-a-peak-element-ii/
`

/**
 * @param {number[][]} mat
 * @return {number[]}
 */
var findPeakGrid = function (mat) {
  const m = mat.length, n = mat[0].length

  let left = -1, right = m - 1
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    const mi = findMaxIndex(mat[mid])
    // 如果当前行最大值的位置比下一行相同位置大，说明最大值在上面的行
    if (mat[mid][mi] > mat[mid + 1][mi]) {
      right = mid
    } else {
      left = mid
    }
  }
  return [right, findMaxIndex(mat[right])]
};

// 返回数组里最大值的下标
function findMaxIndex(arr) {
  let curMaxIndex = 0
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > arr[curMaxIndex]) {
      curMaxIndex = i
    }
  }
  return curMaxIndex
}